#!/bin/bash
# 初始化centos服务器

#必须是root用户才可以执行
if [ $UID -ne 0 ]; then
    echo "Superuser privileges are required to run this script."
    echo "e.g. \"sudo $0\""
    exit 1
fi

#设置一下主机名
echo -n "please set the host name："
read hostname

if [[ ! $hostname ]]; then
  echo "$0 hostname is required"
  exit 1
fi

echo "setting host name to $hostname"
hostnamectl --static --transient --pretty set-hostname $hostname

echo -n "please set the domain："
read domain
echo "export domain=${domain}" >> /etc/environment

# 是否是dev模式
echo -n "is dev mode?(y/n): "
read devmode

resourcebasepath=$(cd `dirname $0`; cd ../resource; pwd) #/root/resource

yum -y install net-tools ntp lvm2 wget firewalld device-mapper nginx

systemctl enable ntpd
systemctl enable firewalld
systemctl start
systemctl enable nginx.service
systemctl start nginx.service

# 将http端口开放
firewall-cmd --add-service=http --permanent

# 安装tomcat，清华大学源，据说速度很快
echo "starting to install tomcat8"
cd $resourcebasepath
cd tomcat

tar -C /usr -xzf apache-tomcat-8.0.47.tar.gz

catalinaHome=`ls /usr/apache-tomcat* -d`

# tomcat空实例
cp tomcat_base.tar.gz ${catalinaHome}/
#  systemctl管理需要用到的tomcat.service
cp tomcat.service ${catalinaHome}/bin/

chown -R root:root ${catalinaHome}
chmod +x ${catalinaHome}/bin/*.sh

# jdbc
cd $resourcebasepath
cd jdbc
cp *.jar ${catalinaHome}/lib

# 设置环境变量
echo "export CATALINA_HOME=${catalinaHome}" >> /etc/environment

# 安装jdk
# 开发模式中jenkins不认openJdk，所以装sun公司的标准jdk
if [[ ${devmode} = 'y' ]]; then
    echo "starting to install jdk1.8"
    cd $resourcebasepath
    cd java
    rpm -ivh jdk-8u151-linux-x64.rpm
    # 设置环境变量
    echo "export JAVA_HOME=/usr/java/jdk1.8.0_151" >> /etc/environment
    echo "export JRE_HOME=/usr/java/jdk1.7.0_79/jre" >> /etc/environment
else
    yum -y install java-1.8.0-openjdk
    echo "export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk" >> /etc/environment
fi

# 静态资源目录
if [[ ! -e /data1 ]]; then
    mkdir /data1
    if [[ ! -e /data1/cdn ]]; then
        mkdir /data1/cdn
    fi
fi

# 安装nginx
echo "starting to install nginx"
cd ${resourcebasepath}/nginx
# nginx 依赖
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget https://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxf nginx-1.12.2.tar.gz
cd nginx*
./configure --user=nginx --group=nginx
make && make install
# 设置资源
cd /usr/local/nginx/conf
mkdir conf.d
cp ${resourcebasepath}/nginx/upload-resource.conf conf.d
sed -i -e "s@#{domain}@"${domain}"@g"\
 conf.d/upload-resource.conf
# 创建用户
useradd nginx
cd sbin
./nginx


# 是否是dev模式，安装开发中需要的东东
if [[ ${devmode} = 'y' ]]; then
    # 安装jenkins，需要设置端口，8088
    echo "starting to install jenkins"
    wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
    rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    yum -y install jenkins
    # 开放8088端口
    firewall-cmd --zone=public --add-port=8088/tcp --permanent

    # 安装禅道
    echo "starting to install zentao"
    cd $resourcebasepath
    cd zentao
    tar -zxvf ZenTaoPMS.9.6.1.zbox_64.tar.gz -C /opt
    cd /opt/zbox/
    # 设置禅道端口
    ./zbox -ap 8089
    ./zbox -mp 3307
    # 启动禅道
    ./zbox start
    # 开放8089端口
    firewall-cmd --zone=public --add-port=8089/tcp --permanent
    firewall-cmd --reload

    # 创建一个用来分享资源的用户
    useradd res
    echo "res123456" |passwd --stdin res
    cd /data1/cdn
    mkdir dev
    chown res dev/
    chown :res dev/
    usermod -aG res nginx
fi





